package com.example.demo.interceptor;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.example.demo.utils.JWTUtils;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class JWTInterceptor implements HandlerInterceptor {
    @Override
    public  boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        try {
            JWTUtils.verify(token);
        }catch (SignatureVerificationException e){
            e.printStackTrace();
            System.out.println("无效签名");
            return false;
        }catch (TokenExpiredException e){
            e.printStackTrace();
            System.out.println("token过期");
            return false;
        }catch (AlgorithmMismatchException e){
            e.printStackTrace();
            System.out.println("token算法不一致");
            return false;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("token无效");
            return false;
        }
        return true;
    }
}
